package org.wattdepot.cli.commands;

import java.util.List;
import org.wattdepot.cli.UserCommandInterface;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.resource.source.jaxb.Source;

/**
 * Lists the public sources in the system.
 * 
 * @author Jarret Mizo, Shaun Ramento
 * 
 */
public class ListSources extends UserCommandInterface implements UserCommands {

  /**
   * Prints a table summarizing the public sources in the system, one per line.
   * @param arg String command from input.
   * @return A list of all the sources.
   */
  public String doCommand(String arg) {
    // String that will be returned
    StringBuffer sourcesList = new StringBuffer(256);
    // Outputs into the order of Name, Parent, and Description
    sourcesList.append("Name\t\tParent\t\tDescription\n");
    // Lists the sources for each client
    List<Source> sources;
    try {
      sources = getClient().getSources();
      // For every source it should have the output of the Name, Parent, and Description
      for (Source source : sources) {
        // Name of the client
        String name = source.getName();
        // Description of the client
        String description = source.getDescription();
        // Depends on the length it will tab or not
        int nameLength = name.length();

        if (name.equalsIgnoreCase("SIM_OAHU_GRID")) {
          sourcesList.append(name + "\t\t\t" + description + "\n");
        }

        else if (name.equalsIgnoreCase("SIM_KAHE") || name.equalsIgnoreCase("SIM_WAIAU")
            || name.equalsIgnoreCase("SIM_HONOLULU")) {
          sourcesList.append(name + "\t" + "SIM_OAHU_GRID" + "\t" + description + "\n");
        }

        else if (name.equalsIgnoreCase("SIM_IPP")) {
          sourcesList.append(name + "\t\tSIM_OAHU_GRID\t" + description + "\n");
        }

        else if ((name.equalsIgnoreCase("SIM_AES"))) {
          sourcesList.append(name + "\t\tSIM_IPP\t\t" + description + "\n");
        }

        else if ((name.equalsIgnoreCase("SIM_HPOWER")) || (name.equalsIgnoreCase("SIM_KALAELOA"))) {
          sourcesList.append(name + "\tSIM_IPP\t\t" + description + "\n");
        }

        else if (name.substring(4, nameLength - 2).equalsIgnoreCase("HONOLULU")) {
          sourcesList.append(name + "\t" + name.substring(0, nameLength - 2) + "\t" + description
              + "\n");
        }

        else if (name.substring(4, nameLength - 2).equalsIgnoreCase("KAHE")) {
          sourcesList.append(name + "\t" + name.substring(0, nameLength - 2) + "\t" + description
              + "\n");
        }

        else if (name.substring(4, nameLength - 2).equalsIgnoreCase("WAIAU")) {
          sourcesList.append(name + "\t" + name.substring(0, nameLength - 2) + "\t" + description
              + "\n");
        }
      }
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // return sourceList
    return sourcesList.toString();
  }
}
